package com.zkingsoft.actions;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.model.Equip;
import com.zkingsoft.model.Matain;
import com.zkingsoft.plugin.util.HttpUtils;
import com.zkingsoft.plugin.util.PropertiesUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @管理系统Controller
 */
@Controller
@RequestMapping(value = "/monitor")
public class MonitorController extends BaseController {

	/**
	 * @页面定向方法
	 */
	@RequestMapping(value = "/redirect/{page1}")
	public String redirect(@PathVariable("page1") String page1) {
		return "monitor/" + page1;
	}
	
	/**
	 * @页面定向方法
	 */
	@RequestMapping(value = "/redirect/{page1}/{page2}")
	public String redirect(@PathVariable("page1") String page1, @PathVariable("page2") String page2, 
			HttpServletRequest request) {
		if("allEquip".equals(page1) && !"allEquipList".equals(page2)) {
			String serialNum = request.getParameter("serialNum");
			request.setAttribute("serialNum", serialNum);
		}
		return "monitor/" + page1 + "/" + page2;
	}
	
	/**
	 * @格力设备列表
	 */
	@RequestMapping(value = "/managernewdevlist")
	public @ResponseBody Object managernewdevlist(String serialNum) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		String postUrl = PropertiesUtils.get("config/JSONInterface", "managerdevdelete");
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		postData.add(new BasicNameValuePair("serialNum", serialNum));
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			//JSONObject json = JSONObject.fromObject(body);
			map.put("data", body);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	/**
	 * @设备删除
	 */
	@RequestMapping(value = "/managerdevdelete")
	public @ResponseBody Object managerdevdelete(String serialNum) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		String postUrl = PropertiesUtils.get("config/JSONInterface", "managerdevdelete");
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		postData.add(new BasicNameValuePair("serialNum", serialNum));
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			//JSONObject json = JSONObject.fromObject(body);
			map.put("data", body);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	/**
	 * @设备新增
	 */
	@RequestMapping(value = "/managerdevAdd")
	public @ResponseBody Object managerdevAdd(Equip equip) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		String postUrl = PropertiesUtils.get("config/JSONInterface", "managerdevAdd");
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		
		// 获取实体类的所有属性，返回Field数组  
		Field[] field = equip.getClass().getDeclaredFields();  
		// 遍历所有属性  
		for (int j = 0; j < field.length; j++) {  
		    // 获取属性的名字  
		    String name = field[j].getName();  
		    // 将属性的首字符大写，方便构造get，set方法  
		    String Name = name.substring(0, 1).toUpperCase() + name.substring(1);  
		    String value = "";
		    try {
			    Method m = equip.getClass().getMethod("get" + Name);  
	            // 调用getter方法获取属性值  
	            value = (String) m.invoke(equip);  
		    } catch(Exception e) {
		    	continue;
		    }
		    postData.add(new BasicNameValuePair(name, value));
		}
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			//JSONObject json = JSONObject.fromObject(body);
			map.put("data", body);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	/**
	 * @设备修改
	 */
	@RequestMapping(value = "/managerdevUpdate")
	public @ResponseBody Object managerdevUpdate(Equip equip) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		String postUrl = PropertiesUtils.get("config/JSONInterface", "managerdevUpdate");
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		
		// 获取实体类的所有属性，返回Field数组  
		Field[] field = equip.getClass().getDeclaredFields();  
		// 遍历所有属性  
		for (int j = 0; j < field.length; j++) {  
		    // 获取属性的名字  
		    String name = field[j].getName();  
		    // 将属性的首字符大写，方便构造get，set方法  
		    String Name = name.substring(0, 1).toUpperCase() + name.substring(1);  
		    String value = "";
		    try {
			    Method m = equip.getClass().getMethod("get" + Name);  
	            // 调用getter方法获取属性值  
	            value = (String) m.invoke(equip);  
		    } catch(Exception e) {
		    	continue;
		    }
		    postData.add(new BasicNameValuePair(name, value));
		}
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			//JSONObject json = JSONObject.fromObject(body);
			map.put("data", body);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	
	/**
	 * @维保详情
	 */
	@RequestMapping(value = "/managermataindetail")
	public @ResponseBody Object managermataindetail(String id) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		String postUrl = PropertiesUtils.get("config/JSONInterface", "managermataindetail");
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();  
		postData.add(new BasicNameValuePair("id", id));
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			JSONObject json = JSONObject.fromObject(body);
			map.put("data", json);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	/**
	 * @维保删除
	 */
	@RequestMapping(value = "/managermataindelete")
	public @ResponseBody Object managerMataindelete(String id) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		String postUrl = PropertiesUtils.get("config/JSONInterface", "managermataindelete");
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		postData.add(new BasicNameValuePair("id", id));
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			//JSONObject json = JSONObject.fromObject(body);
			map.put("data", body);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	/**
	 * @维保修改
	 */
	@RequestMapping(value = "/managerMatainUpdate")
	public @ResponseBody Object managerMatainUpdate(Matain matain, String designer) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		String postUrl = PropertiesUtils.get("config/JSONInterface", "managerMatainUpdate");
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		
		// 获取实体类的所有属性，返回Field数组  
		Field[] field = matain.getClass().getDeclaredFields();  
		// 遍历所有属性  
		for (int j = 0; j < field.length; j++) {  
		    // 获取属性的名字  
		    String name = field[j].getName();  
		    // 将属性的首字符大写，方便构造get，set方法  
		    String Name = name.substring(0, 1).toUpperCase() + name.substring(1);  
		    String value = "";
		    try {
			    Method m = matain.getClass().getMethod("get" + Name);  
	            // 调用getter方法获取属性值  
	            value = (String) m.invoke(matain);  
		    } catch(Exception e) {
		    	continue;
		    }
		    postData.add(new BasicNameValuePair(name, value));
		}
		postData.add(new BasicNameValuePair("designer", designer));
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			//JSONObject json = JSONObject.fromObject(body);
			map.put("data", body);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	/**
	 * @用户管理-列表
	 */
	@RequestMapping(value = "/manageruserMana")
	public @ResponseBody Object manageruserMana(String require, int pageSize, int curPageNO) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		String postUrl = PropertiesUtils.get("config/JSONInterface", "manageruserMana");
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		postData.add(new BasicNameValuePair("require", require));
		postData.add(new BasicNameValuePair("pageSize", String.valueOf(pageSize)));
		postData.add(new BasicNameValuePair("curPageNO", String.valueOf(curPageNO)));
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			JSONObject json = JSONObject.fromObject(body);
			map.put("data", json);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	/**
	 * @用户管理-详情
	 */
	@RequestMapping(value = "/manageruserdetail")
	public @ResponseBody Object manageruserdetail(String userId) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		String postUrl = PropertiesUtils.get("config/JSONInterface", "manageruserdetail");
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		postData.add(new BasicNameValuePair("userId", userId));
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			//String str = "{\"userName\":\"test\",\"roleId\":\"普通用户\",\"phone\":\"110\",\"mail\":\"110@teg.cn\"}";
			JSONObject json = JSONObject.fromObject(body);
			map.put("data", json);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	/**
	 * @用户管理-修改
	 */
	@RequestMapping(value = "/manageruserUpdate")
	public @ResponseBody Object manageruserUpdate(String userId,String userName,String phone,String mail,String roleId) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		String postUrl = PropertiesUtils.get("config/JSONInterface", "manageruserUpdate");
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		postData.add(new BasicNameValuePair("userId", userId));
		postData.add(new BasicNameValuePair("userName", userName));
		postData.add(new BasicNameValuePair("mail", mail));
		postData.add(new BasicNameValuePair("phone", phone));
		postData.add(new BasicNameValuePair("roleId", roleId));
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			//JSONObject json = JSONObject.fromObject(body);
			map.put("data", body);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	/**
	 * @用户管理-新增
	 */
	@RequestMapping(value = "/manageruserAdd")
	public @ResponseBody Object manageruserAdd(String userId,String userName,String phone,String mail,String roleId,String passWord) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		String postUrl = PropertiesUtils.get("config/JSONInterface", "manageruserAdd");
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		postData.add(new BasicNameValuePair("userId", userId));
		postData.add(new BasicNameValuePair("userName", userName));
		postData.add(new BasicNameValuePair("mail", mail));
		postData.add(new BasicNameValuePair("phone", phone));
		postData.add(new BasicNameValuePair("roleId", roleId));
		postData.add(new BasicNameValuePair("passWord", passWord));
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			//JSONObject json = JSONObject.fromObject(body);
			map.put("data", body);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	/**
	 * @用户管理-删除
	 */
	@RequestMapping(value = "/manageruserdelete")
	public @ResponseBody Object manageruserdelete(String userId) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		String postUrl = PropertiesUtils.get("config/JSONInterface", "manageruserdelete");
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		postData.add(new BasicNameValuePair("userId", userId));
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			//JSONObject json = JSONObject.fromObject(body);
			map.put("data", body);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	
	/**
	 * @邮件发送
	 */
	@RequestMapping(value = "/mailUserList")
	public @ResponseBody Object mailUserList(String require, String type) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		String postUrl = PropertiesUtils.get("config/JSONInterface", "mailUserList");
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		require = StringUtils.isBlank(require) ? "" : require;
		type = StringUtils.isBlank(type) ? "1" : type;
		postData.add(new BasicNameValuePair("require", require));
		postData.add(new BasicNameValuePair("type", type));
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			JSONArray json = JSONArray.fromObject(body);
			map.put("data", json);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	/**
	 * @邮件发送
	 */
	@RequestMapping(value = "/managermesspush")
	public @ResponseBody Object managermesspush(String receiver, String mailtheme, String mailcontent, String fileString, String fileFormat) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		String postUrl = PropertiesUtils.get("config/JSONInterface", "managermesspush");
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		postData.add(new BasicNameValuePair("receiver", receiver));
		postData.add(new BasicNameValuePair("mailtheme", mailtheme));
		postData.add(new BasicNameValuePair("mailcontent", mailcontent));
		postData.add(new BasicNameValuePair("fileString", fileString));
		postData.add(new BasicNameValuePair("fileFormat", fileFormat));
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			JSONObject json = JSONObject.fromObject(body);
			map.put("data", json);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
}
